package com.such.kit.database.bean;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.such.kit.database.dialect.TypeFactory;
import com.such.kit.database.support.DDLBean;
import com.such.kit.string.StringCommons;
import com.such.kit.validate.ValidateCommons;

public class DDLPrimaryKey extends DDLBean<List<DatabasePrimaryKey>> {

	private static final long serialVersionUID = 1565855007480536857L;

	private String schema;
	private String tableName;
	private String pkName;
	private List<String> pkColumnNames = new ArrayList<String>();

	// 主键信息扩展
	private String changedPkName;

	public DDLPrimaryKey() {
		
	}

	public DDLPrimaryKey(List<DatabasePrimaryKey> primaryKeys) {
		converter(primaryKeys);
	}

	public String getSchema() {
		return this.schema;
	}

	public void setSchema(String schema) {
		this.schema = schema;
	}

	public String getTableName() {
		return this.tableName;
	}

	public void setTableName(String tableName) {
		this.tableName = tableName;
	}

	public String getPkName() {
		return this.pkName;
	}

	public void setPkName(String pkName) {
		this.pkName = pkName;
	}

	public List<String> getPkColumnNames() {
		return this.pkColumnNames;
	}

	public void setPkColumnNames(List<String> pkColumnNames) {
		this.pkColumnNames = pkColumnNames;
	}

	public void addPkColumnName(String pkColumnName) {
		this.pkColumnNames.add(pkColumnName);
	}

	public String getChangedPkName() {
		return this.changedPkName;
	}

	public void setChangedPkName(String changedPkName) {
		this.changedPkName = changedPkName;
	}

	public void converter(List<DatabasePrimaryKey> primaryKeys) {
		if (ValidateCommons.isNotEmpty(primaryKeys)) {
			boolean first = true;
			String checkKey = "";
			for (DatabasePrimaryKey primaryKey : primaryKeys) {
				String schema = primaryKey.getSchema();
				String tableName =primaryKey.getTableName();
				String pkName = primaryKey.getPkName();
				String key = StringCommons.merger(schema, tableName, pkName);
				if (first) {
					first = false;
					checkKey = StringCommons.merger(schema, tableName, pkName);
					setSchema(schema);
					setTableName(tableName);
					setPkName(pkName);
				}
				// 校验列是否属于一个主键
				if (key.equals(checkKey)) {
					addPkColumnName(primaryKey.getColumnName());
				}
			}
		}
	}

	public Map<String, String> buildRendererParams(Map<String, String> resourceKeyMapping, Map<String, String> ddlTemplateMapping, Map<String, String> params, TypeFactory type) {
		String tableName = this.tableName;
		if (ValidateCommons.isNotEmpty(this.schema) && ValidateCommons.isNotEmpty(tableName)) {
			tableName = StringCommons.merger(this.schema, ".", tableName);
		}

		Map<String, String> primaryKeyParams = new HashMap<String, String>();
		if (ValidateCommons.isNotEmpty(params)) {
			primaryKeyParams.putAll(params);
		}
		putString("tableName", tableName, primaryKeyParams, resourceKeyMapping, ddlTemplateMapping);
		putString("pkName", this.pkName, primaryKeyParams, resourceKeyMapping, ddlTemplateMapping);
		putList("pkColumnNames", this.pkColumnNames, primaryKeyParams, resourceKeyMapping, ddlTemplateMapping);
		return primaryKeyParams;
	}

	/**
	 * <p>方法名：grouping</p>
	 * <p>描述：根据 schema、tableName、pkName 进行分组</p>
	 * @param primaryKeys DatabasePrimaryKey
	 * @return 分组列表
	 */
	public static Map<String, List<DatabasePrimaryKey>> grouping(List<DatabasePrimaryKey> primaryKeys) {
		Map<String, List<DatabasePrimaryKey>> primaryKeysMap = new HashMap<String, List<DatabasePrimaryKey>>();
		if (ValidateCommons.isNotEmpty(primaryKeys)) {
			Collections.sort(primaryKeys);
			for (DatabasePrimaryKey primaryKey : primaryKeys) {
				String schema = primaryKey.getSchema();
				String tableName =primaryKey.getTableName();
				String pkName = primaryKey.getPkName();
				String key = StringCommons.merger(schema, tableName, pkName);
				List<DatabasePrimaryKey> tempPrimaryKeys = primaryKeysMap.get(key);
				if (ValidateCommons.isEmpty(tempPrimaryKeys)) {
					tempPrimaryKeys = new ArrayList<DatabasePrimaryKey>();
				}
				tempPrimaryKeys.add(primaryKey);
				primaryKeysMap.put(key, tempPrimaryKeys);
			}
		}
		return primaryKeysMap;
	}

}
